package com.football.fragment.news;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.football.R;
import com.football.adapter.GroupListAdapter;
import com.football.model.Group;

@SuppressLint("ValidFragment")
public class NewsFragment extends Fragment {

	FragmentActivity activity;
	View view;
	Context context;
	ArrayList<Group> groupList;

	public NewsFragment() {

	}

	public NewsFragment(FragmentActivity act) {
		this.activity = act;
		groupList = new ArrayList<Group>();
		Resources res = activity.getResources();
		
		for (int i = 0; i < 14; i++) {
			Group gr = new Group();
			gr.img = new ImageView(act);
			switch (i) {
			case 0:
				gr.img.setImageDrawable(res.getDrawable(R.drawable.ball));
				gr.article = "An Important Message From the Writer";			
				break;
			case 1:
				gr.img.setImageDrawable(res.getDrawable(R.drawable.ball));
				gr.article = "How to follow the guide";				
				break;
			case 2:
				gr.img.setImageDrawable(res.getDrawable(R.drawable.ball));
				gr.article = "Think you have a great idea";				
				break;
			case 3:
				gr.img.setImageDrawable(res.getDrawable(R.drawable.ball));
				gr.article = "If you are an absolute beginner";				
				break;

			default:
				gr.img.setImageDrawable(res.getDrawable(R.drawable.ball));
				gr.article = "If you are an absolute beginner";				
				break;
			}
			
			groupList.add(gr);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_news, null);
		context = view.getContext();

		ListView listView = (ListView)view.findViewById(R.id.groupListView);
		GroupListAdapter adapter = new GroupListAdapter(activity, groupList);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
	            @Override
	            public void onItemClick(AdapterView<?> parent, View view, int position,
	                    long id) {
	                
	            	Fragment fragment = new DetailFragment();
//	        		FragmentManager fragmentManager = activity.getSupportFragmentManager();
	        		FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
	        		Bundle args = new Bundle();  
	                args.putInt(DetailFragment.ARG_POSITION, position); 
	                fragment.setArguments(args);
	                transaction.replace(R.id.frame_container, fragment);
//	                transaction.addToBackStack(null);
	                transaction.commit();
	                
	            }
	        });
		return view;
	}
	
	
}
